<template>
  <div id="sellerOrderData">
    <el-table :data="sellersOrders" border stripe style="width: 100%">
      <el-table-column prop="_id" label="订单编号"> </el-table-column>
      <el-table-column prop="ordersTime" label="创建时间"> </el-table-column>
      <el-table-column prop="customersId.name" label="订单客户">
      </el-table-column>
      <el-table-column prop="ordersName" label="受益人">
      </el-table-column>
      <el-table-column prop="payPhone" label="支付手机"> </el-table-column>
      <el-table-column
        prop="ordersPayState"
        :formatter="PayState"
        label="订单状态"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            v-show="scope.row.ordersPayState == 4"
            @click="handleRelieve(scope.$index, scope.row)"
            >退保申请</el-button
          >
          <p v-show="scope.row.ordersPayState != 4" type="info">
            {{ PayState(scope.$index, scope.row) }}
          </p>
        </template>
      </el-table-column>
      <el-table-column
        prop="ordersState"
        :formatter="State"
        label="理赔状态"
      ></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <div>
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >修改
            </el-button>
            <el-dialog
              title="修改订单信息"
              :visible.sync="changeOrder"
              width="30%"
              center
            >
              <el-form
                ref="form"
                :model="ordersData"
                label-width="80px"
                size="mini"
              >
                <div class="input-box">
                  <div class="admins-input">
                    <el-form-item label="产品编号">
                      <el-tag type="info">{{ scope.row._id }}</el-tag>
                    </el-form-item>
                    <el-form-item label="产品名称">
                      <el-tag type="info">{{
                        scope.row.insuresId.name
                      }}</el-tag>
                    </el-form-item>
                    <el-form-item label="订单日期">
                      <el-tag type="info">{{ scope.row.ordersTime }}</el-tag>
                    </el-form-item>
                    <el-form-item label="受益人">
                      <el-input v-model="ordersData.ordersName"></el-input>
                    </el-form-item>
                    <el-form-item label="支付状态">
                      <el-tag v-if="scope.row.ordersPayState == 0" type="info"
                        >已支付</el-tag
                      >
                      <el-tag
                        v-else-if="scope.row.ordersPayState == 1"
                        type="info"
                        >未支付</el-tag
                      >
                      <el-tag
                        v-else-if="scope.row.ordersPayState == 2"
                        type="info"
                        >退保中</el-tag
                      >
                      <el-tag
                        v-else-if="scope.row.ordersPayState == 3"
                        type="info"
                        >已退保</el-tag
                      >
                      <el-tag v-else type="info">退保申请</el-tag>
                    </el-form-item>
                    <el-form-item label="理赔状态">
                      <el-tag v-if="scope.row.ordersState == 0" type="info"
                        >未理赔</el-tag
                      >
                      <el-tag v-else-if="scope.row.ordersState == 1" type="info"
                        >理赔申请中</el-tag
                      >
                      <el-tag v-else-if="scope.row.ordersState == 2" type="info"
                        >理赔中</el-tag
                      >
                      <el-tag v-else-if="scope.row.ordersState == 3" type="info"
                        >已理赔</el-tag
                      >
                    </el-form-item>
                    <el-form-item label="客户电话">
                      <el-input v-model="ordersData.payPhone"></el-input>
                    </el-form-item>
                  </div>
                </div>
                <el-form-item size="large">
                  <el-button type="primary" size="mini" @click="upload()"
                    >修改</el-button
                  >
                  <el-button size="mini" @click="changeOrder = false"
                    >取消</el-button
                  >
                </el-form-item>
              </el-form>
            </el-dialog>
          </div>
         
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const {
  mapActions: mapActionsSeller,
  mapState: mapStateSeller,
  mapMutations: mapMutationsSeller,
} = createNamespacedHelpers("Sellers");
const {
  mapActions: mapActionsOrders,
  mapState: mapStateOrders,
  mapMutations: mapMutationsOrders,
} = createNamespacedHelpers("Orders");
export default {
  data() {
    return {
      id: localStorage.salesmansId,
      centerDialogVisible: false,
      changeOrder: false,
      insurances: [],
      ordersData: "",
    };
  },

  computed: {
    ...mapStateSeller(["sellersOrders"]),
  },

  methods: {
    ...mapActionsOrders(["submitOrdersToCancel", "changeOrderById"]),
    ...mapActionsSeller(["getSellersOrderById"]),

    handleSee(index, row) {
      this.insurances = row.insurancesId;
      this.centerDialogVisible = true;
    },

    PayState(index, row) {
      switch (row.ordersPayState) {
        case 0: {
          return "已支付";
          break;
        }
        case 1: {
          return "未支付";
          break;
        }
        case 2: {
          return "退保中";
          break;
        }
        case 3: {
          return "已退保";
          break;
        }
        case 4: {
          return "退保申请";
          break;
        }
      }
    },

    State(row, column) {
      switch (row.ordersState) {
        case 0: {
          return "未理赔";
          break;
        }
        case 1: {
          return "理赔申请中";
          break;
        }
        case 2: {
          return "理赔中";
          break;
        }
        case 3: {
          return "已理赔";
          break;
        }
      }
    },

    handleRelieve(index, row) {
      this.$confirm("此操作将提交退保申请, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "已提交",
            duration: "800",
          });
          this.submitOrdersToCancel(row._id);
          this.getSellersOrderById({
            _id: this.id,
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作",
            duration: "800",
          });
        });
    },

    handleEdit(index, row) {
      this.ordersData = row;
      this.changeOrder = true;
    },

  

    async upload() {
      const data = await this.changeOrderById(this.ordersData);
      switch (data.status) {
        case 1: {
          this.$message({
            duration: "1500",
            type: "success",
            message: "修改成功",
          });
          this.changeOrder = false;
          break;
        }
        case 0: {
          this.$message({
            duration: "1500",
            type: "warning",
            message: "未改动",
          });
          break;
        }
        case 2: {
          this.$message({
            duration: "1500",
            type: "error",
            message: "修改失败,该账号已存在",
          });
          break;
        }
      }
    },
  },
};
</script>

<style lang="scss">
#sellerOrderData {
  .el-form {
    padding: 30px 0;
    .input-box {
      display: flex;
      justify-content: space-evenly;
      .el-input {
        width: 260px;
      }
      .img-upload {
        h3 {
          margin-bottom: 10px;
        }
        text-align: center;
      }
    }
    .el-form-item__content {
      margin: 0;
    }
  }
}
</style>